package com.sq.partner.manager.util;

import java.security.MessageDigest;

import org.apache.log4j.Logger;

public class MD5Util {
	public static final String md5Key = "CIC_PARTNER_*#";
	
	private static Logger logger = LogUtil.getLog(MD5Util.class);
	public static String md5(String input){
		String pwd = md5Ogn(input);
		//return md5Ogn(getSubStr(pwd));//这种方式如果找不到子串索引可能自己也不能确定密码了
		return md5Ogn(pwd + md5Key );
	}
	public static String md5One(String input){
		return md5Ogn(input);
	}
	private static String md5Ogn(String input){
		 char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};       
	        try {
	            byte[] btInput = input.getBytes();
	            // 获得MD5摘要算法的 MessageDigest 对象
	            MessageDigest mdInst = MessageDigest.getInstance("MD5");
	            // 使用指定的字节更新摘要
	            mdInst.update(btInput);
	            // 获得密文
	            byte[] md = mdInst.digest();
	            // 把密文转换成十六进制的字符串形式
	            int j = md.length;
	            char str[] = new char[j * 2];
	            int k = 0;
	            for (int i = 0; i < j; i++) {
	                byte byte0 = md[i];
	                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
	                str[k++] = hexDigits[byte0 & 0xf];
	            }
	            return new String(str);
	        } catch (Exception e) {
	            logger.error("加密失败", e);
	            return null;
	        }


	}
	
	//{1,2,3,4,6,7,8,9,12,13,15,16,19,20,21,25,28,31}
		private static final int[] subIdx = {1,2,3,4,6,7,8,9,12,13,15,16,19,20,21,25,28,31};
		private static String getSubStr(String str){
			StringBuffer sb = new StringBuffer();
			for(int i : subIdx){
				sb.append(str.charAt(i));
			}
			return sb.toString();
		}
	
	public static void main(String[] args) {
		System.out.println(md5("123456")); 

	}

}
